home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XLINE.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  5KB  |  320 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XLINE
  3. ;
  4. ; Line drawing functions.
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ; ****** Aeronautical Research Laboratory              ****************
  14. ; ****** Defence Science and Technology Organisation   ****************
  15. ; ****** Australia                                     ****************
  16. ;
  17. ; egg@dstos3.dsto.gov.au
  18. ; teg@bart.dsto.gov.au
  19. ;-----------------------------------------------------------------------
  20. include xlib.inc
  21. include xline.inc
  22.  
  23.     .code
  24.  
  25.  
  26. ModeXAddr       macro
  27.     mov    cl,bl
  28.     push    dx
  29.     mov    dx,[_ScrnLogicalByteWidth]
  30.     mul    dx
  31.     pop    dx
  32.     shr    bx,2
  33.     add    bx,ax
  34.     add     bx,[PgOffs]
  35.     and    cl,3
  36.     endm
  37.  
  38.  
  39. ;-----------------------------------------------------------------------
  40. ; _x_line
  41. ;
  42. ; Line drawing function for all MODE X 256 Color resolutions
  43. ; Based on code from "PC and PS/2 Video Systems" by Richard Wilton.
  44. ;
  45. ; Compile with Tasm.
  46. ; C callable.
  47. ;
  48.  
  49. _x_line     proc
  50. ARG     x1:word,y1:word,x2:word,y2:word,Color:word,PgOffs:word
  51. LOCAL   vertincr:word,incr1:word,incr2:word,routine:word=LocalStk
  52.     push    bp        ; Set up stack frame
  53.     mov    bp,sp
  54.     sub    sp,LocalStk
  55.     push    si
  56.     push    di
  57.  
  58.     mov    ax,0a000h
  59.     mov    es,ax
  60.  
  61.     mov    dx,SC_INDEX    ; setup for plane mask access
  62.  
  63. ; check for vertical line
  64.  
  65.     mov    si,[_ScrnLogicalByteWidth]
  66.     mov    cx,[x2]
  67.     sub    cx,[x1]
  68.     jz    VertLine
  69.  
  70. ; force x1 < x2
  71.  
  72.     jns    L01
  73.  
  74.     neg    cx
  75.  
  76.     mov    bx,[x2]
  77.     xchg    bx,[x1]
  78.     mov    [x2],bx
  79.  
  80.     mov    bx,[y2]
  81.     xchg    bx,[y1]
  82.     mov    [y2],bx
  83.  
  84. ; calc dy = abs(y2 - y1)
  85.  
  86. L01:
  87.     mov    bx,[y2]
  88.     sub    bx,[y1]
  89.     jnz    short skip
  90.     jmp     HorizLine
  91. skip:        jns    L03
  92.  
  93.     neg    bx
  94.     neg    si
  95.  
  96. ; select appropriate routine for slope of line
  97.  
  98. L03:
  99.     mov    [vertincr],si
  100.     mov    [routine],offset LoSlopeLine
  101.     cmp    bx,cx
  102.     jle    L04
  103.     mov    [routine],offset HiSlopeLine
  104.     xchg    bx,cx
  105.  
  106. ; calc initial decision variable and increments
  107.  
  108. L04:
  109.     shl    bx,1
  110.     mov    [incr1],bx
  111.     sub    bx,cx
  112.     mov    si,bx
  113.     sub    bx,cx
  114.     mov    [incr2],bx
  115.  
  116. ; calc first pixel address
  117.  
  118.     push    cx
  119.     mov    ax,[y1]
  120.     mov    bx,[x1]
  121.     ModeXAddr
  122.     mov    di,bx
  123.     mov    al,1
  124.     shl    al,cl
  125.     mov    ah,al        ; duplicate nybble
  126.     shl    al,4
  127.     add    ah,al
  128.     mov    bl,ah
  129.     pop    cx
  130.     inc    cx
  131.     jmp    [routine]
  132.  
  133. ; routine for verticle lines
  134.  
  135. VertLine:
  136.     mov    ax,[y1]
  137.     mov    bx,[y2]
  138.     mov    cx,bx
  139.     sub    cx,ax
  140.     jge    L31
  141.     neg    cx
  142.     mov    ax,bx
  143.  
  144. L31:
  145.     inc    cx
  146.     mov    bx,[x1]
  147.     push    cx
  148.     ModeXAddr
  149.  
  150.     mov    ah,1
  151.     shl    ah,cl
  152.     mov    al,MAP_MASK
  153.     out    dx,ax
  154.     pop    cx
  155.     mov    ax, word ptr [Color]
  156.  
  157. ; draw the line
  158.  
  159. L32:
  160.     mov    es:[bx],al
  161.     add    bx,si
  162.     loop    L32
  163.     jmp    Lexit
  164.  
  165. ; routine for horizontal line
  166.  
  167. HorizLine:
  168.     push    ds
  169.  
  170.     mov    ax,[y1]
  171.     mov    bx,[x1]
  172.     ModeXAddr
  173.  
  174.     mov    di,bx     ; set dl = first byte mask
  175.     mov    dl,00fh
  176.     shl    dl,cl
  177.  
  178.     mov    cx,[x2] ; set dh = last byte mask
  179.     and    cl,3
  180.     mov    dh,00eh
  181.     shl    dh,cl
  182.     not    dh
  183.  
  184. ; determine byte offset of first and last pixel in line
  185.  
  186.     mov    ax,[x2]
  187.     mov    bx,[x1]
  188.  
  189.     shr    ax,2     ; set ax = last byte column
  190.     shr    bx,2    ; set bx = first byte column
  191.     mov    cx,ax    ; cx = ax - bx
  192.     sub    cx,bx
  193.  
  194.     mov    ax,dx    ; mov end byte masks to ax
  195.     mov    dx,SC_INDEX ; setup dx for VGA outs
  196.     mov     bx, [Color]
  197.  
  198. ; set pixels in leftmost byte of line
  199.  
  200.     or    cx,cx      ; is start and end pt in same byte
  201.     jnz    L42        ; no !
  202.     and    ah,al      ; combine start and end masks
  203.     jmp    short L44
  204.  
  205. L42:            push    ax
  206.     mov     ah,al
  207.     mov     al,MAP_MASK
  208.     out     dx,ax
  209.     mov    al,bl
  210.     stosb
  211.     dec    cx
  212.  
  213. ; draw remainder of the line
  214.  
  215. L43:
  216.     mov    ah,0Fh
  217.     mov    al,MAP_MASK
  218.     out    dx,ax
  219.     mov    al,bl
  220.     rep    stosb
  221.     pop     ax
  222.  
  223. ; set pixels in rightmost byte of line
  224.  
  225. L44:
  226.     mov    al,MAP_MASK
  227.     out    dx, ax
  228.     mov     byte ptr es:[di],bl
  229.     pop    ds
  230.     jmp    short Lexit
  231.  
  232.  
  233. ; routine for dy >= dx (slope <= 1)
  234.  
  235. LoSlopeLine:
  236.     mov    al,MAP_MASK
  237.     mov    bh,byte ptr [Color]
  238. L10:
  239.     mov    ah,bl
  240.  
  241. L11:
  242.     or    ah,bl
  243.     rol    bl,1
  244.     jc    L14
  245.  
  246. ; bit mask not shifted out
  247.  
  248.     or    si,si
  249.     jns    L12
  250.     add    si,[incr1]
  251.     loop    L11
  252.  
  253.     out    dx,ax
  254.     mov    es:[di],bh
  255.     jmp    short Lexit
  256.  
  257. L12:
  258.     add    si,[incr2]
  259.     out    dx,ax
  260.     mov    es:[di],bh
  261.     add    di,[vertincr]
  262.     loop    L10
  263.     jmp    short Lexit
  264.  
  265. ; bit mask shifted out
  266.  
  267. L14:            out    dx,ax
  268.     mov    es:[di],bh
  269.     inc    di
  270.     or    si,si
  271.     jns    L15
  272.     add    si,[incr1]
  273.     loop    L10
  274.     jmp    short Lexit
  275.  
  276. L15:
  277.     add    si,[incr2]
  278.     add    di,[vertincr]
  279.     loop    L10
  280.     jmp    short Lexit
  281.  
  282. ; routine for dy > dx (slope > 1)
  283.  
  284. HiSlopeLine:
  285.     mov    bx,[vertincr]
  286.     mov    al,MAP_MASK
  287. L21:            out    dx,ax
  288.     push    ax
  289.     mov    ax,[Color]
  290.     mov    es:[di],al
  291.     pop    ax
  292.     add    di,bx
  293.  
  294. L22:
  295.     or    si,si
  296.     jns    L23
  297.  
  298.     add    si,[incr1]
  299.     loop    L21
  300.     jmp    short Lexit
  301.  
  302. L23:
  303.     add    si,[incr2]
  304.     rol    ah,1
  305.     adc    di,0
  306. lx21:    loop    L21
  307.  
  308. ; return to caller
  309.  
  310. Lexit:
  311.     pop    di
  312.     pop    si
  313.     mov    sp,bp
  314.     pop    bp
  315.     ret
  316.  
  317. _x_line    endp
  318.  
  319. end
  320.